/**
 * @filename:CapitalTypeServiceImpl 2023-4-20
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.MoldAllocation;
import com.wonder.app.domain.MoldInspect;
import com.wonder.app.domain.MoldMaintenance;
import com.wonder.app.domain.MoldMaterial;
import com.wonder.app.mapper.MoldAllocationDao;
import com.wonder.app.mapper.MoldMaintenanceDao;
import com.wonder.app.mapper.MoldMaterialDao;
import com.wonder.app.request.MoldAllocationRequest;
import com.wonder.app.request.MoldMaintenanceRequest;
import com.wonder.app.service.*;
import com.wonder.common.utils.MapToEntity;
import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import com.wonder.framework.aspect.enums.BusinessStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:TODO(模具调拨单类型服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 *
 */
@Service

public class MoldMaintenanceServiceImpl extends ServiceImpl<MoldMaintenanceDao, MoldMaintenance> implements MoldMaintenanceService {

    @Autowired
    private MoldMaintenanceService moldMaintenanceService;
    @Autowired
    private MoldMaintenanceDao moldMaintenanceDao;
    @Autowired
    private MoldAndInspectService moldAndInspectService;
    @Autowired
    private MoldMaterialService moldMaterialService;

    @Autowired
    private MoldMaterialDao moldMaterialDao;

    @Override
    public String validate(MoldMaintenance moldMaintenance, String type) {
        return "";
    }

    @Override
    public IPage<MoldMaintenance> selectList(MoldMaintenanceRequest request, Page<MoldMaintenance> page) {
        LambdaQueryWrapper<MoldMaintenance> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (null != request.getId()) {
            lambdaQueryWrapper.eq(MoldMaintenance::getId, request.getId());
        }
        if (request.getBillNo() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getBillNo,request.getBillNo());
        }
        if (request.getInspectNo() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getInspectNo,request.getInspectNo());
        }
        if (request.getInStockNo() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getInStockNo,request.getInStockNo());
        }
        if (request.getMaterialNo() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getMaterialNo,request.getMaterialNo());
        }
        if (request.getMaterialName() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getMaterialName,request.getMaterialName());
        }
        if (request.getStockNo() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getStockNo,request.getStockNo());
        }
        if (request.getStockName() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getStockName,request.getStockName());
        }
        if (request.getCreateUserName() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getCreateUserName,request.getCreateUserName());
        }
        if (request.getRepairName() != null){
            lambdaQueryWrapper.like(MoldMaintenance::getRepairName,request.getRepairName());
        }
        if (request.getCreateTime() != null){
            Date[] orderDate = request.getCreateTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(MoldMaintenance::getCreateTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(MoldMaintenance::getCreateTime,orderDate[1]);

        }
        if (request.getRepairStartTime() != null){
            Date[] orderDate = request.getRepairStartTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(MoldMaintenance::getRepairStartTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(MoldMaintenance::getRepairStartTime,orderDate[1]);

        }
        if (request.getRepairEndTime() != null){
            Date[] orderDate = request.getRepairEndTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(MoldMaintenance::getRepairEndTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(MoldMaintenance::getRepairEndTime,orderDate[1]);

        }
        lambdaQueryWrapper.orderByDesc(MoldMaintenance::getId);
        Page<MoldMaintenance> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    /**
     * 检验改模具能欧大修
     * @param map
     * @return
     */
    @Override
    public boolean checkMainTen(Map<String,Object> map){
        int id = VtuzxUtil.getInt(map,"id");
        int erpCl = 0;
        MoldMaterial moldMaterial = moldMaterialService.getById(id);
        if(moldMaterial == null){
            throw new RuntimeException("未查询到模具数据");
        }else {
            erpCl = moldMaterial.getErpCl();
        }
        if(erpCl == 1){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 生成维修单
     * @param userName
     * @param
     * @return
     */
    @Override
    public boolean createMainTen(MoldInspect moldInspect, int moldId, String userName){
        String BILLNO =  IXqcConst.createBillNo(BillType.MJWX);//生成模具维修单号
        MoldMaintenance moldMaintenanc =new MoldMaintenance();
        moldMaintenanc.setBillNo(BILLNO);
        moldMaintenanc.setCreateTime(new Date());
        moldMaintenanc.setCreateUserName(userName);
        moldMaintenanc.setMaterialNo(moldInspect.getMaterialNo());
        moldMaintenanc.setMaterialName(moldInspect.getMaterialName());
        moldMaintenanc.setLocationNo(moldInspect.getLocationNo());
        moldMaintenanc.setLocationName(moldInspect.getLocationName());
        moldMaintenanc.setInspectNo(moldInspect.getBillNo());
        moldMaintenanc.setCategory(moldInspect.getCategory());//维修类别
        moldMaintenanc.setStatus(1);//维修状态1未完成
        moldMaintenanc.setMoldId(moldId);
        moldMaintenanc.setStockNo(moldInspect.getStockNo());
        moldMaintenanc.setStockName(moldInspect.getStockName());
        boolean save = moldMaintenanceService.save(moldMaintenanc);

        Map<String,Object> insMap = new HashMap<>();
        insMap.put("moldId",moldId);
        insMap.put("orderTime",moldMaintenanc.getCreateTime());
        insMap.put("orderNo",BILLNO);
        insMap.put("orderType",2);//维修单类型
        moldAndInspectService.createAndInspect(userName,insMap);

        return save;
    }

    /**
     * 填写写维修单信息后修改
     * @param userName
     * @param map
     * @return
     */
    @Override
    public boolean updateMainTen(String userName, Map<String, Object> map){
        MoldMaintenance moldMaintenance = moldMaintenanceService.getById(VtuzxUtil.getInt(map,"id"));
        moldMaintenance.setRepairName(VtuzxUtil.getString(map,"RepairName"));
        moldMaintenance.setCategory(VtuzxUtil.getInt(map,"Category"));//维修类型大修小修
        moldMaintenance.setRemark(VtuzxUtil.getString(map,"Remark"));//备注
        moldMaintenanceService.saveOrUpdate(moldMaintenance);
        //map中增加类型
        map.put("OrderType",2);
        map.put("type","维修出库");
        map.put("status",1);
        //创建关联关系
        boolean ins = moldAndInspectService.createAndInspect(userName,map);
        //修改模具链状态
//        boolean mold = moldMaterialService.updateMoldStatus(map);
        return ins;
    }

    /**
     * 模具检查单列表
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> getMoldMainList(Map<String, Object> map) {
        int pageIndex = VtuzxUtil.getInt(map,"pageIndex");
        int pageSize = VtuzxUtil.getInt(map,"pageSize");
        String billNo = VtuzxUtil.getString(map,"billNo");//订单号
        List<MoldMaintenance> dataList = moldMaintenanceDao.getMoldMainList(pageSize,pageIndex * pageSize,billNo);
        Map<String,Object> data = new HashMap<>();
        data.put("dataList",dataList);
        return data;
    }

    /**
     * 模具检查单详情
     * @param map
     * @param user_id
     * @return
     */
    @Override
    public Map<String, Object> getMoldMainDetail(Map<String, Object> map, Long user_id) {
        String billNo = VtuzxUtil.getString(map,"billNo");
        MoldMaintenance moldMaintenance = moldMaintenanceDao.selectOne(new LambdaQueryWrapper<MoldMaintenance>()
                .eq(MoldMaintenance::getBillNo, billNo));
        if (VtuzxUtil.isEmpty(moldMaintenance)){
            throw new RuntimeException("单号错误请重试");
        }
        Map<String,Object> data = new HashMap<>();
        data.put("data",moldMaintenance);

        return data;
    }

    /**
     * 审核模具维修单
     * @param map
     * @param user_id
     * @return
     * @throws Exception
     */
    @Override
    public Boolean auditMoldMain(Map<String, Object> map, Long user_id) throws Exception {
        MoldMaintenance moldMaintenance = MapToEntity.mapToBean(map,MoldMaintenance.class);
        //直接修改检查单的信息，重置模具的使用次数
        moldMaintenance.setStatus(2);

        int moldId = moldMaintenance.getMoldId();
        MoldMaterial moldMaterial = moldMaterialDao.selectById(moldId);
        boolean res = moldMaterialService.updateMoldStatus(moldMaterial, "维修完成");

        boolean b = moldMaintenanceService.updateById(moldMaintenance);

        return res;
    }
}
